/*
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

/*
document looks like:

<document
     xmlns     = 'http://code.google.com/p/garmin-view'>
    <configuration version='0.0.2'>
      <garmintools_directory>/home/willi/.garmin</garmintools_directory>
    </configuration>
    <configuration version='0.0.1'>
      <garmintools_directory>/home/willi/.garmin</garmintools_directory>
    </configuration>
</document>
*/

#include "./xmlconfigreader.h"
#include <QtDebug>

/**
*   Constructor.
*   Opens config file in current directory, named config.xml
*/
XMLConfigReader::XMLConfigReader()
{
    iodevice = new QFile ( "config.xml" );

    if ( !iodevice->open ( QIODevice::ReadOnly ) )
    {
        // TODO: handle errors correctly
        qFatal("Failed to open xml config file.");
    }

    //if (!createDOM())
    //  qFatal("Failed to create DOM from config file.");
}

/**
*   Constructor.
*   @param filename The xml config file to be read.
*/
XMLConfigReader::XMLConfigReader ( QString filename )
{
    iodevice = new QFile(filename);

    if ( !iodevice->open ( QIODevice::ReadOnly ) )
    {
        // TODO: handle errors correctly
        qFatal("Failed to open xml config file.");
    }
}


XMLConfigReader::~XMLConfigReader()
{
  iodevice->close();
  delete(iodevice);
  delete(dom);
}



/**
*   Creates the Document Object Model from the given file.
*   @return True on successful creation.
*/
bool XMLConfigReader::createDOM()
{
    dom = new QDomDocument ( "XmlConfigFile" );

    if ( !dom->setContent ( iodevice ) )
    {
        // TODO: handle errors correctly
        qWarning("Failed setting content of DOM model.");
        return false;
    }

    return true;
}

/**
*   Mutator to set the DOM object of the xml config file.
*   @param dom The DOM object to set.
*   @return True on successful set.
*/
bool XMLConfigReader::setDOM(QDomDocument* dom)
{
//TODO: memory leak if dom is already set
    this->dom = dom;
    return true;
}

/**
*
* @return QString containing value, null if key not found
*/
QString XMLConfigReader::getConfigValue(QString key, QString version)
{
    QDomElement element = dom->documentElement();
    element = element.firstChildElement("configuration");
    
    while(!element.isNull())
    {
        if(element.hasAttribute("version"))
        {
            if(element.attribute("version") == version)
            {
              return element.firstChildElement(key).text();

              if(!element.isNull())
                return element.text();
              return NULL;
            }
        }
        element = element.nextSiblingElement("configuration");
    }
    
    return NULL;
}

/**
@return true if configs match, false otherwise (incl DNE)
*/
bool XMLConfigReader::matchConfigValue(QString key, QString value, QString version)
{
  return value == getConfigValue(key, version);
}


/**
 * A method to match the current configuration version with
 * the program's version.
 * @param version The current version of the program.
 * @return False on no match for current version, true for match.
 */
bool XMLConfigReader::matchConfigVersion(QString version)
{
    QDomElement element = dom->documentElement();
    element = element.firstChildElement("configuration");
    while(!element.isNull())
    {
        if(element.hasAttribute("version"))
        {
            if(element.attribute("version") == version)
            {
            currentConfigElement = element.cloneNode().toElement();
            return true;
            }
        }
        element = element.nextSiblingElement("configuration");
    }

    return false;
}
